package com.wuyan.warriorsoffate.ui.theme

import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.TweenSpec
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.google.accompanist.insets.ProvideWindowInsets
import com.wuyan.warriorsoffate.ui.theme.WeTheme.Theme.*

private val PinkColorPalette = createWeColors(
    bg=PinkBg,
    label1=PinkLabel1,
    label2=PinkLabel2,
    text=PinkText
)

private val GreeneryColorPalette = createWeColors(
    bg=GreeneryBg,
    label1=GreeneryLabel1,
    label2=GreeneryLabel2,
    text=GreeneryText
)

private val AestheticColorPalette = createWeColors(
    bg=AestheticBg,
    label1=AestheticLabel1,
    label2=AestheticLabel2,
    text=AestheticText
)
private val BeautyColorPalette = createWeColors(
    bg=BeautyBg,
    label1=BeautyLabel1,
    label2=BeautyLabel2,
    text=BeautyText
)
private val LocalWeColors = compositionLocalOf {
    PinkColorPalette
}
private val LocalWeTypography= compositionLocalOf {
    typography
}
private val LocalWeShapes= compositionLocalOf {
    Shapes
}
private val LocalWeDp=compositionLocalOf {
    weDp
}
@Stable
object WeTheme {
    val colors: WeColors
        @Composable
        get() = LocalWeColors.current
    val typography: Typography
        @Composable
        @ReadOnlyComposable
        get() = LocalWeTypography.current

    /**
     * Retrieves the current [Shapes] at the call site's position in the hierarchy.
     */
    val shapes: Shapes
        @Composable
        @ReadOnlyComposable
        get() = LocalWeShapes.current
    val weDp:WeDp
        @Composable
        @ReadOnlyComposable
        get() = LocalWeDp.current
    enum class Theme {
        Pink, Greenery, Aesthetic,Beauty
    }

}
@Stable
class WeColors(
    bg: Color,
    label1: Color,
    label2: Color,
    text: Color,
    onBg: Color,
    onLabel1: Color,
    onLabel2: Color,
    onText: Color,
) {
    var bg: Color by mutableStateOf(bg)
        private set
    var label1: Color by mutableStateOf(label1)
        private set
    var label2: Color by mutableStateOf(label2)
        private set
    var text: Color by mutableStateOf(text)
        private set
    var onBg: Color by mutableStateOf(onBg)
        private set
    var onLabel1: Color by mutableStateOf(onLabel1)
        private set
    var onLabel2: Color by mutableStateOf(onLabel2)
        private set
    var onText: Color by mutableStateOf(onText)
        private set
    companion object{
        @Composable
        fun getRandom():Color{
            return when((0..2).random()){
                0 -> WeTheme.colors.label1
                1-> WeTheme.colors.label2
                2-> WeTheme.colors.text
                else -> {WeTheme.colors.text}
            }
        }
    }
}

private fun createWeColors(bg: Color,
           label1: Color,
           label2: Color,
           text: Color):WeColors{
    return WeColors(
        bg=bg,
        label1=label1,
        label2=label2,
        text=text,
        onBg=text,
        onLabel1=text,
        onLabel2=text,
        onText=bg,
    )
}
@Composable
fun WeContentColorFor(backgroundColor: Color):Color{
    when (backgroundColor) {
        WeTheme.colors.bg -> {
            return WeTheme.colors.onBg
        }
        WeTheme.colors.label1 -> {
            return WeTheme.colors.onLabel1
        }
        WeTheme.colors.label2 -> {
            return WeTheme.colors.onLabel2
        }
        WeTheme.colors.text -> {
            return WeTheme.colors.onText
        }
        WeTheme.colors.onBg -> {
            return WeTheme.colors.bg
        }
        WeTheme.colors.onLabel1 -> {
            return WeTheme.colors.label1
        }
        WeTheme.colors.onLabel2 -> {
            return WeTheme.colors.label2
        }
        WeTheme.colors.onText -> {
            return WeTheme.colors.text
        }
        else -> return WeTheme.colors.text
    }
}
@Composable
fun WeTheme(theme: WeTheme.Theme = Pink, typography: Typography=WeTheme.typography, shapes: Shapes=WeTheme.shapes, content: @Composable () -> Unit) {
    val targetColors = when (theme) {
        Pink -> PinkColorPalette
        Greenery -> GreeneryColorPalette
        Aesthetic -> AestheticColorPalette
        Beauty -> BeautyColorPalette
    }
    val bg = animateColorAsState(targetColors.bg, TweenSpec(600))
    val label1 = animateColorAsState(targetColors.label1, TweenSpec(600))
    val label2 = animateColorAsState(targetColors.label2, TweenSpec(600))
    val text = animateColorAsState(targetColors.text, TweenSpec(600))
    val onBg = animateColorAsState(targetColors.onBg, TweenSpec(600))
    val onLabel1 = animateColorAsState(targetColors.onLabel1, TweenSpec(600))
    val onLabel2 = animateColorAsState(targetColors.onLabel2, TweenSpec(600))
    val onText = animateColorAsState(targetColors.onText, TweenSpec(600))
    val colors = WeColors(
        bg=bg.value,
        label1=label1.value,
        label2=label2.value,
        text=text.value,
        onBg=onBg.value,
        onLabel1=onLabel1.value,
        onLabel2=onLabel2.value,
        onText=onText.value,
    )

    CompositionLocalProvider(LocalWeColors provides colors, LocalWeTypography provides typography,
        LocalWeShapes provides shapes) {
        MaterialTheme(
            shapes = shapes
        ) {
            ProvideWindowInsets(content = content)
        }
    }
}